﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions managers
        ////////////////////////FONCTIONS MANAGERS////////////////////////////////


        //Listing des managers enregistrés
        private void list_managers_offline(ListBox maListe)
        {
            maListe.Items.Clear();

            foreach (Managers s in (from e in db_local.Managers select e))
            {
                maListe.Items.Add(s.Login);

            }
        }


        //Affichage de la liste des managers
        private void display_managers_offline(String managers)
        {
            foreach (Managers s in (from e in db_local.Managers select e))
            {
                if (s.Login == managers)
                {
                    manager_login_textbox.Text = s.Login;
                    manager_password_textbox.Password = s.Password;
                    manager_firstname_textbox.Text = s.Firstname;
                    manager_surname_textbox.Text = s.Surname;

                    //Récupération, découpage et affichage des campus enregistrés pour un manager
                    manager_registred_campus_listbox.Items.Clear();

                    string[] table_campus;
                    string registred_campus = s.Campus;
                    table_campus = registred_campus.Split('|');

                    for (int i = 0; i < table_campus.Length; i++)
                    {
                        manager_registred_campus_listbox.Items.Add(table_campus[i]);
                    }

                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_managers_offline()
        {
            manager_login_textbox.Text = "";
            manager_password_textbox.Password = "";
            manager_firstname_textbox.Text = "";
            manager_surname_textbox.Text = "";
            list_managers(manager_listbox);
            manager_registred_campus_listbox.Items.Clear();

            chargement_liste_campus_manager_offline();

        }


        //Ajout d'un manager
        private void add_managers_offline()
        {
            Managers manager1 = new Managers();

            manager1.Login = manager_login_textbox.Text.ToLower();
            manager1.Password = manager_password_textbox.Password;
            manager1.Firstname = manager_firstname_textbox.Text.ToLower();
            manager1.Surname = manager_surname_textbox.Text.ToLower();

            manager1.Notepad_enabled = 1;
            manager1.Notepad_text = "Write some notes here";


            //Concaténation de la sélection de campus puis enregistrement
            string concatenated_campus = "";

            for (int i = 0; i < manager_campus_listbox.SelectedItems.Count; i++)
            {
                string selected_campus = manager_campus_listbox.SelectedItems[i].ToString();

                if (concatenated_campus == "")
                {
                    concatenated_campus = String.Concat(selected_campus);
                }
                else
                {
                    concatenated_campus = String.Concat(concatenated_campus, "|", selected_campus);
                }
            }

            manager1.Campus = concatenated_campus;


            db_local.Managers.InsertOnSubmit(manager1);
            db_local.SubmitChanges();

            //Rafraichissement des éléments
            refresh_managers_offline();


        }


        //Mise à jour d'un manager
        private void update_managers_offline()
        {
            if (manager_login_textbox.Text != "" && manager_password_textbox.Password != "" && manager_firstname_textbox.Text != "" && manager_surname_textbox.Text != "")
            {
                Managers manager2 = (from e in db_local.Managers
                                     where e.Login == (string)manager_listbox.SelectedValue
                                     select e).FirstOrDefault();

                manager2.Login = manager_login_textbox.Text.ToLower();
                manager2.Password = manager_password_textbox.Password;
                manager2.Firstname = manager_firstname_textbox.Text.ToLower();
                manager2.Surname = manager_surname_textbox.Text.ToLower();


                //Concaténation de la sélection de campus puis enregistrement
                string concatenated_campus = "";

                for (int i = 0; i < manager_campus_listbox.SelectedItems.Count; i++)
                {
                    string selected_campus = manager_campus_listbox.SelectedItems[i].ToString();

                    if (concatenated_campus == "")
                    {
                        concatenated_campus = String.Concat(selected_campus);
                    }
                    else
                    {
                        concatenated_campus = String.Concat(concatenated_campus, "|", selected_campus);
                    }
                }

                manager2.Campus = concatenated_campus;


                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_managers_offline();
            }

        }


        //Suppression d'un manager
        private void delete_managers_offline()
        {
            if (manager_login_textbox.Text != "")
            {
                Managers manager3 = (from e in db_local.Managers
                                     where e.Login == (string)manager_listbox.SelectedValue
                                     select e).FirstOrDefault();

                db_local.Managers.DeleteOnSubmit(manager3);
                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_managers_offline();
            }

        }

        private void chargement_liste_campus_manager_offline()
        {
            manager_campus_listbox.Items.Clear();

            foreach (Campus s in (from e in db_local.Campus select e))
            {
                manager_campus_listbox.Items.Add(s.Name);
            }
        }

        #endregion
    }
}
